package com.yqg.protection.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yqg.protection.common.Result;
import com.yqg.protection.common.interceptor.SysLog;
import com.yqg.protection.entity.EquipmentDevice;
import com.yqg.protection.mapper.ClientMapper;
import com.yqg.protection.mapper.EquipmentDeviceMapper;
import com.yqg.protection.service.IEquipmentDeviceService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

/**
 * EquipmentDeviceController 前端控制器
 * @author zxf
 * @since 2024-09-11
 */
@RestController
@RequestMapping("/equipment-device")
public class EquipmentDeviceController {

    @Resource
    private IEquipmentDeviceService equipmentDeviceService;

    @Resource
    private EquipmentDeviceMapper equipmentDeviceMapper;

    @Autowired
    private ClientMapper clientMapper;

    /**
    *  新增或者更新
    */
    @PostMapping("/edit")
    @SysLog("新增或者修改设备设施")
    public Result save(@RequestBody EquipmentDevice equipmentDevice) {
        equipmentDevice.setIsDelete(0);
        equipmentDevice.setOrganization(clientMapper.selectById(equipmentDevice.getClientId()).getOrganization());
        return Result.success(equipmentDeviceService.saveOrUpdate(equipmentDevice));
    }
    /**
    *  删除
    */
    @DeleteMapping("delete/{id}")
    @SysLog("删除设备设施")
    public Result delete(@PathVariable Integer id) {
        EquipmentDevice equipmentDevice =equipmentDeviceService.getById(id);
        equipmentDevice.setIsDelete(1);
        return Result.success(equipmentDeviceService.saveOrUpdate(equipmentDevice));
    }
    /**
    *  查询所有
    */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<EquipmentDevice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
       List<EquipmentDevice> list=  equipmentDeviceService.list(queryWrapper);
        return Result.success(list);
    }
    /**
    *  根据id查询
    */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(equipmentDeviceService.getById(id));
    }
    /**
    *  分页查询
    */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           Integer level,
                           Long organizationId,
                           String deviceName,
                           String clientName,
                           String isAllClient,
                           @RequestParam List<Long> clientIds) {
        pageNum = pageNum * pageSize - pageSize;
        List<EquipmentDevice> equipmentDevices = equipmentDeviceMapper.findPage(pageNum, pageSize, level, organizationId, deviceName, clientName, isAllClient, clientIds);
        Long count = equipmentDeviceMapper.findPageCount(level, organizationId, deviceName, clientName, isAllClient, clientIds);
        HashMap<String, Object> map = new HashMap<>();
        map.put("record", equipmentDevices);
        map.put("total", count);
        return Result.success(map);
    }

    @ApiOperation("导出数据")
    @GetMapping("/export")
    public void export(HttpServletResponse response,
                       String deviceName,
                       String clientName,
                       Integer level,
                       Long organizationId,
                       String isAllClient,
                       @RequestParam List<Long> clientIds) {
        equipmentDeviceService.export(response, organizationId, deviceName, clientName, level, isAllClient, clientIds);
    }

    @ApiOperation("导入数据")
    @PostMapping("/importData")
    @SysLog("导入设备设施")
    public void importData(MultipartFile file) {
        equipmentDeviceService.importData(file);
    }
}

